﻿using System.Text;
using ListAndLINQ;

namespace List;

class Program
{
    static void Main(string[] args)
    {
        // list
        List<Person> list = new List<Person>();
        list.Add(new Person("Alice", 20));
        list.Add(new Person("tomy", 40));
        for (int i = 0; i < list.Count; i++)
        {
            // Console.WriteLine(list[i]);
        }

        //字典
        Dictionary<string, int> dictionary = new Dictionary<string, int>();
        dictionary.Add("you", 12);
        dictionary.Add("sdou", 1232);
        dictionary.Add("bg", 456);
        dictionary.Remove("sdou");
        //键值对
        foreach (KeyValuePair<string, int> kvp in dictionary)
        {
            // Console.WriteLine(kvp.Key + ": " + kvp.Value);
        }

        //遍历键(通过键找到值)
        foreach (string key in dictionary.Keys)
        {
            // Console.WriteLine(key + ": " + dictionary[key]);
        }

        //遍历值
        foreach (int value in dictionary.Values)
        {
            // Console.WriteLine(value);
        }

        //hashSet
        HashSet<int> set = new HashSet<int>();
        set.Add(1);
        set.Add(2);
        set.Add(2);
        set.Add(3);
        Console.WriteLine($"长度：{set.Count}");
        for (int i = 0; i < set.Count; i++)
        {
            // Console.Write(set.ElementAt(i) + " ");
        }
    }

    //两数之和
    public int[] TwoSum(int[] nums, int target)
    {
        /*for (int i = 0; i < nums.Length; i++)
        {
            int temp = target - nums[i];
            for (int j = i + 1; j < nums.Length; j++)
            {
                if (nums[j] == temp)
                {
                    return new int[] { i, j };
                }
            }
        }*/
        //动态数组解法
        /*List<int> list = new List<int>(nums);
        for (int i = 0; i < list.Count; i++)
        {
            int temp = target - list[i];
            for (int j = 0; j < list.Count; j++)
            {
                if (list[j] == temp)
                {
                    return new int[] { i, j };
                }
            }
        }*/
        return new[] { -1, -1 };
    }

    //217存在重复的元素
    public bool ContainsDuplicate(int[] nums)
    {
        // HashSet<int> set = new HashSet<int>();
        // foreach (int num in nums)
        // {
        //     if (!set.Add(num))
        //     {
        //         return true;
        //     }
        // }
        //第二种方法
        HashSet<int> set = new HashSet<int>(nums);
        if (nums.Length > set.Count)
        {
            return true;
        }

        return false;
    }

    //347 前K个高频元素
    public static int[] TopKFrequent(int[] nums, int k)
    {
        Dictionary<int, int> dict = new Dictionary<int, int>();
        foreach (int num in nums)
        {
            dict[num] = dict.GetValueOrDefault(num, 0) + 1;
        }

        PriorityQueue<int, int> queue = new PriorityQueue<int, int>();
        foreach (var entry in dict)
        {
            queue.Enqueue(entry.Key, entry.Value);
            if (queue.Count > k)
            {
                queue.Dequeue();
            }
        }

        int[] res = new int[k];
        for (int i = k - 1; i >= 0; i--)
        {
            res[i] = queue.Dequeue();
        }

        return res;
    }

    //1768 交替合字符串
    public string MergeAlternately(string word1, string word2)
    {
        Queue<char> queue1 = new Queue<char>(word1);
        Queue<char> queue2 = new Queue<char>(word2);
        StringBuilder res = new StringBuilder();
        while (queue1.Count > 0 || queue2.Count > 0)
        {
            if (queue1.Count > 0)
            {
                res.Append(queue1.Dequeue());
            }
            if (queue2.Count > 0)
            {
                res.Append(queue2.Dequeue());
            }
        }
        return res.ToString();
    }

    //205 同构字符串
    public bool IsIsomorphic(string s, string t)
    {
        Dictionary<char, char> s1 = new Dictionary<char, char>();
        Dictionary<char, char> t1 = new Dictionary<char, char>();

        if (s.Length != t.Length)
        {
            return false;
        }

        for (int i = 0; i < s.Length; i++)
        {
            char chS = s[i];
            char chT = t[i];

            if (s1.ContainsKey(chS))
            {
                if (s1[chS] != chT)
                {
                    return false;
                }
            }
            else
            {
                s1.Add(chS, chT);
            }

            if (t1.ContainsKey(chT))
            {
                if (t1[chT] != chS)
                {
                    return false;
                }
            }
            else
            {
                t1.Add(chS, chT);
            }
        }

        return true;
    }
}